﻿using MetroDataApi.Models;
using MetroDataQuery.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi
{
    public class QueryResults
    {
        ///// <summary>
        ///// 获取给定的时间下,某个测项某个位置数据的变化量
        ///// </summary>
        ///// <param name="prjId"></param>
        ///// <param name="dateStr"></param>
        ///// <param name="monitorType"></param>
        ///// <param name="location"></param>
        ///// <returns></returns>
        //public static IEnumerable<result> Query(int prjId, string dateStr, string monitorType, string location)
        //{
        //    var list =
        //    MetroDataQuery.Utility.MonitorPointsQuery.GetPointsValueByTime(prjId, monitorType, location, dateStr, true)
        //    .Select(p => new result()
        //    {
        //        name = p.Name,
        //        time = p.DateTime.Value.ToString("yyyy-MM-dd"),
        //        value = p.Value.Value
        //    }).ToList();
        //    return list;
        //}

        /// <summary>
        /// 获取初始值时间
        /// </summary>
        /// <param name="prjId"></param>
        /// <param name="monitorType"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public static List<result> GetOriginResults(int prjId, string monitorType, string location)
        {
            var list = MonitorPointsQuery.GetOriginResults(prjId, monitorType, location)
                .Select(p => new result()
                {
                    name = p.Name,
                    time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                    value = p.Value.Value
                }).ToList();
            return list;
        }


        /// <summary>
        /// 获取一个点的初始值时间
        /// </summary>
        /// <param name="prjId"></param>
        /// <param name="monitorType"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public static string GetOriginTime(int prjId, string monitorType, string location)
        {
            return MonitorPointsQuery.GetOriginTime(prjId, monitorType, location);
        }




        /// <summary>
        /// 获取给定的时间下,某个测项某个位置数据的变化量
        /// </summary>
        /// <param name="prjId"></param>
        /// <param name="monitorType"></param>
        /// <param name="location"></param>
        /// <param name="dateStr"></param>
        /// <param name="isResult"></param>
        /// <returns></returns>
        public static List<result> GetResultsByDateTime(int prjId, string monitorType,
                        string location, string dateStr, bool isResult = true, bool isDesign = false)
        {
            var time = DateTime.Parse(dateStr);
            List<result> data = null;
            if (time.Hour == 0 && time.Minute == 0 && time.Second == 0)
            {
                data = MetroDataQuery.Utility.MonitorPointsQuery.
                    GetPointsValueByDate(prjId, monitorType, location, dateStr, isResult).Select(p => new result()
                {
                    name = p.Name,
                    time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                    value = p.Value.Value
                }).ToList();
            }
            else
            {
                data = MetroDataQuery.Utility.MonitorPointsQuery.
                    GetPointsValueByTime(prjId, monitorType, location, dateStr, isResult).Select(p => new result()
                {
                    name = p.Name,
                    time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                    value = p.Value.Value
                }).ToList();
            }
        
            if (!isDesign)
            {
                return data;
            }
            else
            {
                return data.Select(p =>
                           new result() { name = p.name, time = p.time, value = Math.Round((p.value - 5.5) * 1000, 2) }).ToList();
            }
        }


        /// <summary>
        /// 获取未审核数据的某日期结果
        /// </summary>
        /// <param name="prjId"></param>
        /// <param name="monitorType"></param>
        /// <param name="location"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static List<result> GetReviewResultsByDateTime(int prjId, string monitorType,
                       string location, DateTime date)
        {
            var data = ReviewMonitorPointsQuery.GetPointsValueByTime(prjId, monitorType, location, date.ToString("yyyy-MM-dd HH:mm:ss"))
                .Select(p => new result()
                {
                    name = p.Name,
                    time = p.DateTime.Value.ToString("yyyy-MM-dd"),
                    value = p.Value.Value
                }).ToList();
            return data;
        }
    }
}